package com.boe.rnmf.pkg.service.impl;

import com.boe.rnmf.core.exception.BusinessException;
import com.boe.rnmf.entity.OrgUserEntity;
import com.boe.rnmf.entity.RoleUserEntity;
import com.boe.rnmf.entity.UserEntity;
import com.boe.rnmf.vo.UserVO;
import com.boe.rnmf.vo.query.UserQueryParam;
import com.boe.rnmf.pkg.mapper.org.write.OrgUserWriter;
import com.boe.rnmf.pkg.mapper.role.write.RoleUserWriter;
import com.boe.rnmf.pkg.mapper.user.read.UserReader;
import com.boe.rnmf.pkg.mapper.user.write.UserWriter;
import com.boe.rnmf.pkg.service.UserService;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);


    @Resource
    private UserReader userReader;

    @Resource
    private UserWriter userWriter;

    @Resource
    private RoleUserWriter roleUserWriter;

    @Resource
    private OrgUserWriter orgUserWriter;

    @Override
    public UserEntity get(String id){
        return userReader.selectByPrimaryKey(id);
    }

    /**
     * 创建用户
     * @param userVO
     * @return
     */
    @Override
    public void saveUser(UserVO userVO) {
        try{
            /**保存用户**/
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(userVO,userEntity);
            userWriter.insertSelective(userEntity);
            /**保存用户角色关系**/
            RoleUserEntity roleUserEntity = new RoleUserEntity();
            roleUserEntity.setUserId(userEntity.getId());
            roleUserEntity.setRoleId(userVO.getRoleId());
            roleUserEntity.setCreateUserId(userVO.getCreateUserId());
            roleUserWriter.insert(roleUserEntity);
            /**保存用户组织关系**/
            OrgUserEntity orgUserEntity = new OrgUserEntity();
            orgUserEntity.setUserId(userEntity.getId());
            orgUserEntity.setOrgId(userEntity.getId());
            orgUserEntity.setCreateUserId(userVO.getCreateUserId());
            orgUserWriter.insert(orgUserEntity);
        }catch (Exception e){
            throw new BusinessException("创建用户失败",e);
        }
    }

    @Override
    public int update(UserEntity userEntity) {
        return userWriter.updateByPrimaryKey(userEntity);
    }

    public int deleteUserById(String userId){
        return userWriter.deleteUserById(userId);
    }

    /**
     * 查询用户
     *
     * @param userQueryParam 查询参数
     * @return
     */
    public List<UserEntity> queryUserList(UserQueryParam userQueryParam) {
        return userReader.queryUserList(userQueryParam);
    }

    /**
     * 冻结用户
     * @param userId 用户Id
     * @param status 用户状态
     * @return
     */
    public int freezeUser(String userId,String status){
        return userWriter.freezeUser(userId,status);
    }
}
